﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyShop
{
    public partial class AddProduct : System.Web.UI.Page
    {
        public int CurrentOrderId;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {

            string saveDir = @"\Uploads\";
            string appPath = Request.PhysicalApplicationPath;

            if (ImageUpload.HasFile)
            {
                string savePath = appPath + saveDir +
                Server.HtmlEncode(ImageUpload.FileName);

                ImageUpload.SaveAs(savePath);

            }
            else
            {
                // Notify the user that a file was not uploaded.
                //UploadStatusLabel.Text = "You did not specify a file to upload.";
            }
        }

        protected void AddProduct_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("InsertProduct", sqlcon);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@GroupID", SqlDbType.Int));
                cmd.Parameters["@GroupID"].Value = GroupProductDropDownList.SelectedValue;
                cmd.Parameters.Add(new SqlParameter("@CompanyID", SqlDbType.Int));
                cmd.Parameters["@CompanyID"].Value = CompanyNameDropDownList.SelectedValue;
                cmd.Parameters.Add(new SqlParameter("@Price", SqlDbType.Int));
                cmd.Parameters["@Price"].Value = Price.Text;
                cmd.Parameters.Add(new SqlParameter("@ModelName", SqlDbType.NVarChar, 1024));
                cmd.Parameters["@ModelName"].Value = ModelName.Text;
                cmd.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar));
                cmd.Parameters["@Description"].Value = Description.Text;
                cmd.Parameters.Add(new SqlParameter("@Count", SqlDbType.Int));
                cmd.Parameters["@Count"].Value = Count.Text;
                cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int, 4));
                cmd.Parameters["@Id"].Direction = ParameterDirection.Output;

                sqlcon.Open();
                try
                {
                    int rdr = cmd.ExecuteNonQuery();
                    CurrentOrderId = Convert.ToInt32(cmd.Parameters["@Id"].Value);
                }
                finally
                {
                    sqlcon.Close();
                }
                AddImages(CurrentOrderId);
            }
        }

        protected void AddImages(int orderId)
        {
            string saveDir = @"\Uploads\" + GroupProductDropDownList.Text + "\\" + CompanyNameDropDownList.Text + "\\";
            string appPath = Request.PhysicalApplicationPath;
            string savePathDirectory = appPath + saveDir;
            string saveUrl = saveDir + orderId + ".jpg";


            if (!Directory.Exists(savePathDirectory))
            {
                Directory.CreateDirectory(savePathDirectory);
            }
            if (ImageUpload.HasFile)
            {
                string savePath = savePathDirectory + orderId + ".jpg";
                ImageUpload.SaveAs(savePath);
            }

            using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "insert into Images(ProductID,ImageFileUrl)values('" + orderId + "','" + saveUrl + "')";
                cmd.Connection = sqlcon;
                sqlcon.Open();
                try
                {
                    cmd.ExecuteNonQuery();
                }
                finally
                {
                    sqlcon.Close();
                }
            }
        }
    }
}